const form = document.querySelector('form');
const oldPassword = document.querySelector('.oldPassword');
const newPassword = document.querySelector('.newPassword');
const rNewPassword = document.querySelector('.rNewPassword');

// 封装一个函数来用提交修改密码的发送请求
async function pwd() {
  // 请求服务器接口校验登录
  // let {info} = await isLogin();
  // let oldpwd = oldPassword.value;
  // let username = info.username;
  // let l = await http.post('/users/login', {username: username, password: oldpwd});
  // console.log(l.data);
  
  // if (l.data.code !== 1) {
  // alert('原始密码错误');
  // return;
  // }

// 校验信息完整性
if (oldPassword.value === '' || newPassword.value === '' || rNewPassword.value === '') {
    alert('请填写完整信息');
    return;
  }
  if (newPassword.value !== rNewPassword.value) {
    alert('两次密码不一致');
    return;
  }
  // 立即执行函数判断是否登录
let id = localStorage.getItem('uid')
  let res = await http.post('/users/rpwd', { id:id, oldPassword: oldPassword.value, newPassword: newPassword.value, rNewPassword: rNewPassword.value })
  console.log(res.data.code);
  if (res.data.code == 1) {
    alert(res.data.message);
    location.href = './login.html';
  } else {
    alert(res.data.message);
  }
}

// 监听提交事件
form.addEventListener('submit', async function (e) {
  e.preventDefault();
  await pwd();
}
)
